//
//  MTableMenu.swift
//  SwiftStudy3.0
//
//  Created by mal on 17/2/10.
//  Copyright © 2017年 maliang. All rights reserved.
//

import UIKit

let reuseIdentifer = "cell"

class MTableMenu: UITableViewController {

    lazy var dataSource = [MMenuItemModel]()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: reuseIdentifer)
    }
    
    
    func addItemWithClass(itemClass:AnyClass,itemDes:String) -> Void {
        
        self.addItem(itemModel: MMenuItemModel.init(itemClass: itemClass, des: itemDes))
    }
    
    func addItem(itemModel:MMenuItemModel) -> Void {
        
        self.dataSource.append(itemModel)
    }

    // MARK: - Table view data source
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        // #warning Incomplete implementation, return the number of rows
        return self.dataSource.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifer)
        
        let model = self.dataSource[indexPath.row]
        cell?.textLabel?.text = model.des
        
        return cell!
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let model = self.dataSource[indexPath.row]
        if let targetVC = model.getItem(){
            
            if model.transationType == .MPUSH {
                
                self.navigationController?.pushViewController(targetVC, animated: true)
            }
            else{
                
                self.present(targetVC, animated: true, completion: nil)
            }
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
